package cn.gson.crm.controller.apply;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.gson.crm.controller.BaseServlet;
import cn.gson.crm.model.dao.ApplyDao;
import cn.gson.crm.model.entity.Apply;
import cn.gson.crm.model.entity.Employee;
import cn.gson.crm.vo.CustomerVo;

/**
 * Servlet implementation class InsertApplyServlet
 */
@WebServlet("/insert/apply")
public class InsertApplyServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	public ApplyDao ad = new ApplyDao();

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		Employee e = (Employee) request.getSession().getAttribute("emp");
		List<Employee> emp = ad.getAllEmployeeByEmpId(e.getEmpId());
		request.setAttribute("id", id);
		request.setAttribute("name", name);
		request.setAttribute("e", e);
		request.setAttribute("emp", emp);
		forward("apply/apply_manager.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void post(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String custId = request.getParameter("custId");
		String custName = request.getParameter("custName");
		String empId = request.getParameter("empId");
		String empName = request.getParameter("empName");
		String acceptId = request.getParameter("acceptId");
		
		Apply apply = new Apply();
		apply.setCust_id(Integer.parseInt(custId));
		apply.setCust_name(custName);
		apply.setAccept_id(Integer.parseInt(acceptId));
		apply.setEmp_id(Integer.parseInt(empId));
		apply.setEmp_name(empName);
		
		Apply a = ad.getApplyByCustIdAndEmpIdAndAcceptId(Integer.parseInt(custId), Integer.parseInt(empId), Integer.parseInt(acceptId));
		if(a != null){
			switch (a.getApply_status_id()) {
			case 1:
				commonMethod(request, response);
				errorForward("你已经发送了援助请求！", "apply/apply_manager.jsp");
				break;
			case 2:
				commonMethod(request, response);
				errorForward("对方已接受你的援助请求！", "apply/apply_manager.jsp");
				break;
			case 3:
				commonMethod(request, response);
				errorForward("对方已拒绝了你的援助请求！", "apply/apply_manager.jsp");
				break;
			default:
				commonMethod(request, response);
				errorForward("双方已经完成了援助请求！", "apply/apply_manager.jsp");
				break;
			}
		}else{
			ad.InsertApply(apply);
			redirect("cust/form");
		}
	}
	
	private void commonMethod(HttpServletRequest request, HttpServletResponse response){
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		Employee e = (Employee) request.getSession().getAttribute("emp");
		List<Employee> emp = null;
		try {
			emp = ad.getAllEmployeeByEmpId(e.getEmpId());
		} catch (SQLException e1) {	
			e1.printStackTrace();
		}
		request.setAttribute("id", id);
		request.setAttribute("name", name);
		request.setAttribute("e", e);
		request.setAttribute("emp", emp);
	}
}
